#include <bits/stdc++.h>
// 2025/07/23
// tag:
// Author: Forever
using namespace std;
using int64 = int64_t;
int n, m, sx, sy, ex, ey;
int dir[4][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
bool vis[51][51][4], grid[51][51];

struct pos {
    int x, y, t, d;
};

bool check(int x, int y, int d)
{
    if (x < 1 || x >= n || y < 1 || y >= m)
        return false;
    return !(grid[x][y] || grid[x + 1][y] || grid[x][y + 1] || grid[x + 1][y + 1]);
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> grid[i][j];
    char c;
    cin >> sx >> sy >> ex >> ey >> c;
    queue<pos> q;
    q.push({ sx, sy, 0, (c == 'E' ? 0 : (c == 'S' ? 1 : (c == 'W' ? 2 : 3))) });
    vis[sx][sy][(c == 'E' ? 0 : (c == 'S' ? 1 : (c == 'W' ? 2 : 3)))] = true;
    while (!q.empty()) {
        pos p = q.front();
        q.pop();
        if (p.x == ex && p.y == ey) {
            cout << p.t << endl;
            return 0;
        }
        for (int i = 1; i <= 3; i++) {
            int x = p.x + dir[p.d][0] * i, y = p.y + dir[p.d][1] * i;
            if (!check(x, y, p.d))
                break;
            if (!vis[x][y][p.d])
                vis[x][y][p.d] = true, q.push({ x, y, p.t + 1, p.d });
        }
        if (!vis[p.x][p.y][(p.d + 3) % 4])
            vis[p.x][p.y][(p.d + 3) % 4] = true, q.push({ p.x, p.y, p.t + 1, (p.d + 3) % 4 });
        if (!vis[p.x][p.y][(p.d + 1) % 4])
            vis[p.x][p.y][(p.d + 1) % 4] = true, q.push({ p.x, p.y, p.t + 1, (p.d + 1) % 4 });
    }
    cout << -1 << endl;
    return 0;
}